package com.example.common.utils;


import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.experimental.Accessors;

import java.util.HashMap;

/**
 * 统一返回值
 *
 * @author   
 * @date 2020/9/8
 */
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Accessors(chain = true)

public class RestResponse<T>  extends HashMap<String, Object> {


	private int code;

	private String msg;

	private String stackTrace;

	private T data;

	/**
	 * 成功
	 *
	 * @param msg  成功消息
	 * @param data 返回的数据
	 *   @author:  mtl   
	 */
	public static <T> RestResponse succeed(String msg, T data) {
		return new RestResponse(RestStatus.SUCCESS.code(), msg, "", data);
	}

	/**
	 * 成功
	 *
	 * @param data 返回的数据
	 *   @author:  mtl   
	 */
	public static <T> RestResponse succeed(T data) {
		return new RestResponse(RestStatus.SUCCESS.code(), "success", "", data);
	}

	/**
	 * 成功
	 *
	 *   @author:  mtl   
	 */
	public static RestResponse succeed() {
		return succeed(null);
	}

	/**
	 * 失败
	 *
	 * @param msg  失败消息
	 * @param data 失败数据
	 *   @author:  mtl   
	 */
	public static <T> RestResponse failed(int code, String msg, T data) {
		code = code == RestStatus.SUCCESS.code() ? RestStatus.BUSINESS_ERROR.code() : code;
		return new RestResponse(code, msg, "", data);
	}

	/**
	 * 失败
	 *
	 * @param msg  失败消息
	 * @param data 失败数据
	 *   @author:  mtl   
	 */
	public static <T> RestResponse failed(int code, String msg, String stackTrace, T data) {
		code = code == RestStatus.SUCCESS.code() ? RestStatus.BUSINESS_ERROR.code() : code;
		return new RestResponse(code, msg, stackTrace, data);
	}

	/**
	 * 失败
	 *
	 * @param msg  失败消息
	 * @param data 失败数据
	 *   @author:  mtl   
	 */
	public static <T> RestResponse failed(String msg, T data) {
		return failed(RestStatus.BUSINESS_ERROR.code(), msg, data);
	}

	/**
	 * 失败
	 *
	 * @param msg 失败消息
	 *   @author:  mtl   
	 */
	public static <T> RestResponse failed(String msg) {
		return failed(msg, null);
	}

	@Override
	public RestResponse put(String key, Object value) {
		super.put(key, value);
		return this;
	}
}
